package com.baidu.fd1931springbootdemo.controller;

import com.baidu.fd1931springbootdemo.bean.ListOfPlayers;
import com.baidu.fd1931springbootdemo.bean.User;
import com.baidu.fd1931springbootdemo.service.ListOfPlayersService;
import com.baidu.fd1931springbootdemo.service.LoginService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
public class MainController {

    @Autowired
    private LoginService loginService;

    @Autowired
    private ListOfPlayersService listOfPlayersService;
//    启动项目根路径访问
    @RequestMapping("/")
    public String login2(){
        return "login";
    }

    @RequestMapping("/login")
    public String login(){
        return "login";
    }

    @RequestMapping(value = "/login",method = RequestMethod.POST)
    public String toLogin(User user,ModelMap modelMap, HttpSession session){
     boolean loginUser= loginService.loginService(user);
        if (loginUser) {
            String username=user.getUsername();
            String password=user.getPassword();
            session.setAttribute("loginUser",new User(username,password));
            return "index";
//        if (username!=null && "admin".equals(username)) {
//            session.setAttribute("loginUser",new User(username,password));
//            return "index";
//        }

        }
        modelMap.put("message", "用户名或者密码错误！");
        return "login";
    }

    @RequestMapping("/main")
    public String toMain(HttpSession session){
        final Object loginUser = session.getAttribute("loginUser");
//        表示没有登录
        if (loginUser == null) {
            return "login";
        }
        return "index";
    }

    @RequestMapping("/logout")
    public String toLogout(User user){
        return "login";
    }

    @RequestMapping("/basicTable")
    public String toBasictable(){
        return "table/basic_table";
    }

    @RequestMapping("/dynamicTable")
    public String toDynamictable(){
        return "table/dynamic_table";
    }

    @RequestMapping("/editableTable")
    public String toEditabletable(ModelMap modelMap){
        List<ListOfPlayers> listOfPlayersService = this.listOfPlayersService.listOfPlayersService();
        modelMap.put("list",listOfPlayersService);
        return "table/editable_table";
    }

    @RequestMapping("/responsiveTable")
    public String toResponsivetable(){
        return "table/responsive_table";
    }
}
